#include <iostream>
#include <string>

#include "commandlinetest.h"
#include "../commandlineoptions.h"

#include "boost/filesystem.hpp"
#include <boost/program_options.hpp>
#include <boost/algorithm/string.hpp>
#include <boost/exception/exception.hpp>

using namespace std;
using namespace cshotel;
using namespace boost;
using namespace boost::program_options;

void CommandlineTest::setUp() {
}

void CommandlineTest::tearDown() {

}

void CommandlineTest::testDefaultValues() {
	
	argc = 1;
	char * argv [1] = {"stitcher"};
	CPPUNIT_ASSERT_NO_THROW	( CommandLineOptions::getInstance().initialize(argc, argv) );
	CPPUNIT_ASSERT ( (int) CommandLineOptions::getInstance().getInputFileNames().size() == 0 ); 
  CPPUNIT_ASSERT ( CommandLineOptions::getInstance().getOutputFormat() == CommandLineOptions::PanoramaFile );
  CPPUNIT_ASSERT ( CommandLineOptions::getInstance().getOutputRenderer() == CommandLineOptions::Enblend );
	CPPUNIT_ASSERT ( CommandLineOptions::getInstance().getEnblendOptions() == "-a -m 500" );
	CPPUNIT_ASSERT ( CommandLineOptions::getInstance().getDeepZoomConverterJar() == "DeepZoomConverter.jar" );
	CPPUNIT_ASSERT ( CommandLineOptions::getInstance().getDeepZoomConverterOptions() == "-verbose" );
	CPPUNIT_ASSERT ( CommandLineOptions::getInstance().getJavaOptions() == "-Xmx512m" );
  CPPUNIT_ASSERT ( CommandLineOptions::getInstance().getWorkingDirectory() == "tmp/" );
	CPPUNIT_ASSERT ( CommandLineOptions::getInstance().getMaximumMatchingVersionImageSize() != NULL );
	CPPUNIT_ASSERT ( CommandLineOptions::getInstance().getContinueProgram() == true );
	CPPUNIT_ASSERT ( CommandLineOptions::getInstance().getCreateThumbnail() == false );
	CPPUNIT_ASSERT ( CommandLineOptions::getInstance().getThumbnailSuffix() == "_thumb" );
	CPPUNIT_ASSERT ( CommandLineOptions::getInstance().getThumbnailSize() == 48 );
}

void CommandlineTest::testHelp() {
	
	argc = 2;
	char * argv [2] = {"stitcher","--help"};
	CPPUNIT_ASSERT_NO_THROW	( CommandLineOptions::getInstance().initialize(argc, argv) );
	CPPUNIT_ASSERT ( (int) CommandLineOptions::getInstance().getInputFileNames().size() == 0 ); 
	CPPUNIT_ASSERT ( CommandLineOptions::getInstance().getMaximumMatchingVersionImageSize() != NULL );
	CPPUNIT_ASSERT ( CommandLineOptions::getInstance().getContinueProgram() == false );


}

void CommandlineTest::testOutputName() {
	
	argc = 3;
	char * argv [3] = {"stitcher","--output-name","imagename"};
	CPPUNIT_ASSERT_NO_THROW	( CommandLineOptions::getInstance().initialize(argc, argv) );
	CPPUNIT_ASSERT ( (int) CommandLineOptions::getInstance().getInputFileNames().size() == 0 ); 
  CPPUNIT_ASSERT ( CommandLineOptions::getInstance().getOutputName() == "imagename" );

}

void CommandlineTest::testOutputFormat() {
	
	argc = 3;
	char * argv [3] = {"stitcher","--output-format","seadragon"};
	CPPUNIT_ASSERT_NO_THROW	( CommandLineOptions::getInstance().initialize(argc, argv) );
	CPPUNIT_ASSERT ( (int) CommandLineOptions::getInstance().getInputFileNames().size() == 0 ); 
  CPPUNIT_ASSERT ( CommandLineOptions::getInstance().getOutputFormat() == CommandLineOptions::SeaDragon );
	argv[2] = "panoramafile";
	CPPUNIT_ASSERT_NO_THROW	( CommandLineOptions::getInstance().initialize(argc, argv) );
	CPPUNIT_ASSERT ( CommandLineOptions::getInstance().getOutputFormat() == CommandLineOptions::PanoramaFile);
	argv[2] = "transformedfiles";
	CPPUNIT_ASSERT_NO_THROW	( CommandLineOptions::getInstance().initialize(argc, argv) );
	CPPUNIT_ASSERT ( CommandLineOptions::getInstance().getOutputFormat() == CommandLineOptions::TransformedFiles);
	argv[2] = "notavalidoption";
	CPPUNIT_ASSERT_NO_THROW	( CommandLineOptions::getInstance().initialize(argc, argv) );
	CPPUNIT_ASSERT ( CommandLineOptions::getInstance().getOutputFormat() == CommandLineOptions::UnknownOutputFormat );
	CPPUNIT_ASSERT ( CommandLineOptions::getInstance().getContinueProgram() == false );

}

void CommandlineTest::testOutputRenderer() {

	argc = 3;
	char * argv [3] = {"stitcher","--output-renderer","enblend"};
	CPPUNIT_ASSERT_NO_THROW	( CommandLineOptions::getInstance().initialize(argc, argv) );
	CPPUNIT_ASSERT ( (int) CommandLineOptions::getInstance().getInputFileNames().size() == 0 ); 
  CPPUNIT_ASSERT ( CommandLineOptions::getInstance().getOutputRenderer() == CommandLineOptions::Enblend );
	argv[2] = "opencv";
	CPPUNIT_ASSERT_NO_THROW	( CommandLineOptions::getInstance().initialize(argc, argv) );
	CPPUNIT_ASSERT ( CommandLineOptions::getInstance().getOutputRenderer() == CommandLineOptions::OpenCV );
	argv[2] = "visionworkbench";
	CPPUNIT_ASSERT_NO_THROW	( CommandLineOptions::getInstance().initialize(argc, argv) );
	CPPUNIT_ASSERT ( CommandLineOptions::getInstance().getOutputRenderer() == CommandLineOptions::VisionWorkbench );
	argv[2] = "notavalidoption";
	CPPUNIT_ASSERT_NO_THROW	( CommandLineOptions::getInstance().initialize(argc, argv) );
	CPPUNIT_ASSERT ( CommandLineOptions::getInstance().getOutputRenderer() == CommandLineOptions::UnknownOutputRenderer );
	CPPUNIT_ASSERT ( CommandLineOptions::getInstance().getContinueProgram() == false );

}

void CommandlineTest::testDirectory() {
	
	argc = 3;
	char * argv [3] = {"stitcher","--working-directory","directoryname"};
	CPPUNIT_ASSERT_NO_THROW	( CommandLineOptions::getInstance().initialize(argc, argv) );
	CPPUNIT_ASSERT ( (int) CommandLineOptions::getInstance().getInputFileNames().size() == 0 ); 
  CPPUNIT_ASSERT ( CommandLineOptions::getInstance().getWorkingDirectory() == "directoryname/" );

}

void CommandlineTest::testEnblendOptions() {

	argc = 3;
	char * argv [3] = {"stitcher","--enblend-options","-a -b -c -d -e -f -g -h -i -j -k"};
	CPPUNIT_ASSERT_NO_THROW	( CommandLineOptions::getInstance().initialize(argc, argv) );
	CPPUNIT_ASSERT ( (int) CommandLineOptions::getInstance().getInputFileNames().size() == 0 ); 
  CPPUNIT_ASSERT ( CommandLineOptions::getInstance().getEnblendOptions() == "-a -b -c -d -e -f -g -h -i -j -k" );

}

void CommandlineTest::testDeepZoomConverterJar() {

	argc = 3;
	char * argv [3] = {"stitcher","--deepzoom-converter-jar","somejarfile.jar"};
	CPPUNIT_ASSERT_NO_THROW	( CommandLineOptions::getInstance().initialize(argc, argv) );
	CPPUNIT_ASSERT ( (int) CommandLineOptions::getInstance().getInputFileNames().size() == 0 ); 
  CPPUNIT_ASSERT ( CommandLineOptions::getInstance().getDeepZoomConverterJar() == "somejarfile.jar" );

}

void CommandlineTest::testDeepZoomConverterOptions() {

	argc = 3;
	char * argv [3] = {"stitcher","--deepzoom-converter-options","-a -b -c -d -e -f -g -h -i -j -k"};
	CPPUNIT_ASSERT_NO_THROW	( CommandLineOptions::getInstance().initialize(argc, argv) );
	CPPUNIT_ASSERT ( (int) CommandLineOptions::getInstance().getInputFileNames().size() == 0 ); 
  CPPUNIT_ASSERT ( CommandLineOptions::getInstance().getDeepZoomConverterOptions() == "-a -b -c -d -e -f -g -h -i -j -k" );

}

void CommandlineTest::testJavaOptions() {

	argc = 3;
	char * argv [3] = {"stitcher","--java-options","-a -b -c -d -e -f -g -h -i -j -k"};
	CPPUNIT_ASSERT_NO_THROW	( CommandLineOptions::getInstance().initialize(argc, argv) );
	CPPUNIT_ASSERT ( (int) CommandLineOptions::getInstance().getInputFileNames().size() == 0 ); 
  CPPUNIT_ASSERT ( CommandLineOptions::getInstance().getJavaOptions() == "-a -b -c -d -e -f -g -h -i -j -k" );

}

void CommandlineTest::testThumbnailSuffix() {

	argc = 3;
	char * argv [3] = {"stitcher","--thumbnail-suffix","_thisisathumbnail"};
	CPPUNIT_ASSERT_NO_THROW	( CommandLineOptions::getInstance().initialize(argc, argv) );
	CPPUNIT_ASSERT ( (int) CommandLineOptions::getInstance().getInputFileNames().size() == 0 ); 
  CPPUNIT_ASSERT ( CommandLineOptions::getInstance().getThumbnailSuffix() == "_thisisathumbnail" );

}

void CommandlineTest::testThumbnailSize() {

	argc = 3;
	char * argv [3] = {"stitcher","--thumbnail-size","10000000"};
	CPPUNIT_ASSERT_NO_THROW	( CommandLineOptions::getInstance().initialize(argc, argv) );
	CPPUNIT_ASSERT ( (int) CommandLineOptions::getInstance().getInputFileNames().size() == 0 ); 
  CPPUNIT_ASSERT ( CommandLineOptions::getInstance().getThumbnailSize() == 10000000 );
	argv[2] = "abcde";
	CPPUNIT_ASSERT_THROW	( CommandLineOptions::getInstance().initialize(argc, argv) , boost::exception);

}

void CommandlineTest::testImageSize() {
	
	argc = 3;
	char * argv [3] = {"stitcher","--maximum-matching-version-image-size","-1"};
	CPPUNIT_ASSERT_NO_THROW	( CommandLineOptions::getInstance().initialize(argc, argv) );
	CPPUNIT_ASSERT ( (int) CommandLineOptions::getInstance().getInputFileNames().size() == 0 );
	CPPUNIT_ASSERT ( CommandLineOptions::getInstance().getMaximumMatchingVersionImageSize() == -1 ); 
	argv[2] = "abcde";
	CPPUNIT_ASSERT_THROW	( CommandLineOptions::getInstance().initialize(argc, argv) , boost::exception);

}
